/*
 * bsp_gpio.c
 *
 *  Created on: 2023年6月28日
 *      Author: mcealt_DS
 */

#include "bsp_gpio.h"
#include "stddef.h"
#include "device_type.h"
/***
 *@brief led 初始化
 *@param led_ins led 对象
*/
void ds_gpio_init(ds_gpio_t *led_ins)
{
  if(led_ins != NULL) {
      CMU_ClockEnable(cmuClock_GPIO, true);
      GPIO_PinModeSet(led_ins->port,
                      led_ins->pin,
                      led_ins->mode,
                      led_ins->initial);
  }
}

/***
 *@brief 点亮 led
 *@param led_ins led 对象
*/
void ds_gpio_on(ds_gpio_t *led_ins)
{
  if(led_ins != NULL) {
      if(led_ins->valid == 0x00){
          GPIO_PinOutClear(led_ins->port, led_ins->pin);
      }
      else {
          GPIO_PinOutSet(led_ins->port, led_ins->pin);
      }
  }
}

void ds_gpio_set(ds_gpio_t *gpio_ins)
{
  //GPIO_PinOutSet(gpio_ins->port, gpio_ins->pin);
  gpio_ins->mode = gpioModePushPull;
  gpio_ins->initial = 1;
  ds_gpio_init(gpio_ins);//考虑引脚可能被初始化为其他模式，重新初始化模式，推挽输出用，温湿度用不要修改
}

void ds_gpio_clear(ds_gpio_t *gpio_ins)
{
  //GPIO_PinOutClear(gpio_ins->port, gpio_ins->pin);
  gpio_ins->mode = gpioModePushPull;//gpioModeWiredAnd;
  gpio_ins->initial = 0;
  ds_gpio_init(gpio_ins); //考虑引脚可能被初始化为其他模式，重新初始化模式
}

uint8_t ds_gpio_read(ds_gpio_t *gpio_ins)
{
  return (uint8_t)GPIO_PinInGet(gpio_ins->port, gpio_ins->pin);
}

/***
 *@brief 熄灭led
 *@param led_ins led 对象
*/
void ds_gpio_off(ds_gpio_t *led_ins)
{
  if(led_ins != NULL) {
      if(led_ins->valid == 0x00){
          GPIO_PinOutSet(led_ins->port, led_ins->pin);
      }
      else {
          GPIO_PinOutClear(led_ins->port, led_ins->pin);
      }
  }
}

/***
 *@brief 反转led状态
 *@param led_ins led 对象
*/
void ds_gpio_toggle(ds_gpio_t *led_ins)
{
  GPIO_PinOutToggle(led_ins->port, led_ins->pin);
}

/***
 *@brief button 初始化
 *@param button_ins， button 对象
*/
void ds_button_init(ds_button_t *button_ins)
{
  CMU_ClockEnable(cmuClock_GPIO, true);
  GPIO_PinModeSet(button_ins->port,
                  button_ins->pin,
                  button_ins->mode,
                  button_ins->initial);

  GPIOINT_Init();
  button_ins->interruptIndex = GPIOINT_CallbackRegisterExt(button_ins->pin,
                                          (GPIOINT_IrqCallbackPtrExt_t)button_ins->ds_button_state_change,
                                          button_ins);
  GPIO_ExtIntConfig(button_ins->port,
                    button_ins->pin,
                    button_ins->interruptIndex,
                    true,
                    true,
                    true);
}

/***
 *@brief button 初始化
 *@param button_ins， button 对象
*/
void ds_button_deinit(ds_button_t *button_ins)
{
  CMU_ClockEnable(cmuClock_GPIO, true);
  GPIO_ExtIntConfig(button_ins->port,
                    button_ins->pin,
                    button_ins->interruptIndex,
                    false,
                    false,
                    false);
  GPIO_PinModeSet(button_ins->port,
                  button_ins->pin,
                  gpioModeDisabled,
                  0);
}
/***
 *@brief 获取button当前状态
 *@param button_ins， button 对象
 *@return 按键当前的输入状态
*/
uint8_t read_button_level(ds_button_t *button_ins)
{
  return (uint8_t)GPIO_PinInGet(button_ins->port, button_ins->pin);
}
